// pages/my_comment/index.js
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picBaseURL:getApp().picBaseURL,
    imageBaseURL: getApp().imageBaseURL,
    imageResizeParams: getApp().nameimageResizeParams,
    currentDate: new Date().getTime(),//年龄
    minDate: new Date(1900).getTime(),//年龄
    maxDate: new Date().getTime(),//
    agePicker:false,//年龄弹框
    mask:false,
    typepop:{
      sexPop:false,
      tuanyuanPop:false
    },
  
    submitData:{
      headimg:'',//头像
      name:'',//用户名
      nickname:'',//昵称
      tel:'',//电话
      age:'',//出生年月 
      gender:'',//性别（1.男，2女）
      gendertext:'',
      isty:'',//是否团员（1.是，2不是）
      istytext:'',
      address:'',//地址
      zy:'',//职业
      jj:'',//简介
    }
  },
  //点击显示年龄-年份选择器
  showAgePicker(){
    this.setData({
      agePicker:true
    })
  },
  //选择年龄
  confirm(event) {
    let date = new Date(event.detail)
    let now = new Date()
    let year = date.getFullYear();
    let nowYear = now.getFullYear();  
    this.data.submitData.age = nowYear - year;
    this.setData({
      submitData: this.data.submitData,
      agePicker:false
    });
  },
  cancel(){
    this.setData({
      agePicker:false
    });
  },
  
  //获取用户已添信息
  async getUserInfo(){
    let data={}
    data['flag'] = 'v2index_wd_grzl'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      console.log(result)
      let fileList = []
      if(result.headimg != ''){
        fileList.push({url:result.headimg})
      }
      let data = {
        id:result.id,
        headimg:result.headimg,//头像
        name:result.name,//用户名
        nickname:result.nickname,//昵称
        tel:result.tel,//电话
        age:result.age,//出生年月
        gender:result.gender,//性别（1.男，2女）
        gendertext:result.gender == 1 ? '男' : '女',
        isty:result.isty,//是否团员（1.是，2不是）
        istytext:result.isty == 1 ? '是' : '否',
        address:result.address,//地址
        zy:result.zy,//职业
        jj:result.jj,//简介
      }
      this.setData({
        submitData:data,
        fileList:fileList
      })
    }else {
      Toast(message)
    }
  },
  //保存
  async save(){
    let data=this.data.submitData
    console.log(data)
    data['flag'] = 'v2index_wd_grzlsave'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast.success('保存成功')
      wx.navigateBack({
        delta: 1,
      })
    }else {
      Toast(message)
    }

  },
  //输入框输入内容
  changeInput(e){
    let key = e.currentTarget.dataset.key;
    this.data.submitData[key] = e.detail.value
    this.setData({
      submitData:this.data.submitData
    })
  },
  //性别 团圆选择
  selected(e){
    let valkey = e.currentTarget.dataset.valkey
    let textkey = e.currentTarget.dataset.textkey
    let val = e.currentTarget.dataset.val
    let text = e.currentTarget.dataset.text

    this.data.submitData[valkey] = val;
    this.data.submitData[textkey] = text;
    
    let typepop = {
      sexPop:false,
      tuanyuanPop:false
    }
    this.setData({
      typepop,
      mask:false,
      submitData:this.data.submitData
    })

  },
  //性别 团圆下拉框显示隐藏
  showPop(e){
    let type = e.currentTarget.dataset.type
    let type1 = e.currentTarget.dataset.type1
    let show = e.currentTarget.dataset.show
    this.data.typepop[type] = !show;
    this.data.typepop[type1] = false;
    this.setData({
      mask:true,
      typepop:this.data.typepop
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})